package com.cg.wechat;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cg.pojo.TbWeChatUser;
import com.cg.service.TbWeChatUserService;
import com.cg.util.MessageUtils;
import com.cg.util.TextToXmlUtils;
import com.cg.util.TuLingUtils;
import com.cg.util.WeiXinUtils;
import com.cg.wechatvo.dialog.RespTextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/11.
 */
@Service
public class CoreServiceImpl implements CoreService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Reference(version = "1.0.0")
    private TbWeChatUserService tbWeChatUserService;
    @Transactional
    public String dealResponse(HttpServletRequest request) {
        Map<String, String> requestMap = MessageUtils.parseXml(request);
        String fromUserName=requestMap.get("FromUserName"); //发送方账号
        String  toUserName=requestMap.get("ToUserName");        // 开发者账号
        String msgType=requestMap.get("MsgType");         //消息类型
        System.out.println("发送人OpenId:"+fromUserName);
        RespTextMessage textMessage=new RespTextMessage();
           textMessage.setFromUserName(toUserName);
           textMessage.setToUserName(fromUserName);
           textMessage.setCreateTime(new Date().getTime());
           textMessage.setMsgType("text");
           System.out.println("发送消息的类型:"+msgType);
           if(MessageUtils.RESP_MESSAGE_TYPE_TEXT.equals(msgType)) { //文本消息
               try {
                   String string = TuLingUtils.getContent(requestMap.get("Content"));
                   com.alibaba.fastjson.JSONObject object = com.alibaba.fastjson.JSONObject.parseObject(string);
                   textMessage.setContent(object.getString("text").replace("图灵机器人", "cg"));
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
        if(MessageUtils.REQ_MESSAGE_TYPE_EVENT.equals(msgType)) {   //msgType为event
            if (MessageUtils.EVENT_TYPE_SUBSCRIBE.equals(requestMap.get("Event"))) {  //关注
                RespTextMessage rtm = new RespTextMessage();
                rtm.setFromUserName(toUserName);
                rtm.setToUserName(fromUserName);
                rtm.setCreateTime(new Date().getTime());
                rtm.setMsgType("text");
                rtm.setContent("感谢你关注【请叫我中路杀神】\n本人微信号：cg0303003\n精彩多多，么么哒!");
                String res = TextToXmlUtils.textMessageToXml(rtm);
                TbWeChatUser tbWeChatUser = WeiXinUtils.getTbWeChatUser(stringRedisTemplate, fromUserName);
                tbWeChatUser.setSubscribe(1);
                tbWeChatUserService.insert(tbWeChatUser);
                return res;
            }
        }
        return TextToXmlUtils.textMessageToXml(textMessage);
    }
}
